********************************************************************************
** 	TITLE:		UK66_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       1918-2019election_results_by_pcon         		              **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "UK66_results.smcl", replace

* Import the dataset

import excel "1918-2019election_results_by_pcon.xlsx", cellrange(B5:X634) sheet(1966) clear

* Reshape

drop G J M P S V

rename B id
rename C constituency_name
rename D county
rename E region
rename F electorate
rename H votes1
rename I share1
rename K votes2
rename L share2
rename N votes3
rename O share3
rename Q votes4
rename R share4
rename T votes5
rename U share5
rename W totalvotes
rename X turnout

reshape long votes share, i(id) j(party_code)  

gen party_name = " "

replace party_name = "Conservative" if party_code == 1 
replace party_name = "Labour" if party_code == 2
replace party_name = "Liberal" if party_code == 3
replace party_name = "Nationalists" if party_code == 4
replace party_name = "Other" if party_code == 5

drop party_code

gen party_code = .

replace party_code = 1 if party_name == "Conservative"
replace party_code = 2 if party_name == "Labour"
replace party_code = 3 if party_name == "Liberal"
replace party_code = 4 if party_name == "Nationalists"
replace party_code = 5 if party_name == "Other"
replace party_code = -9999 if region == "Northern Ireland"
 
* Generate rank of parties

gsort constituency_name -votes
bysort constituency_name : gen rank = _n

gen first = party_name if rank == 1
gen second = party_name if rank == 2
gen third = party_name if rank == 3

bysort constituency_name : gen winner66 = first[1]
bysort constituency_name : gen runnerup66 = second[2]
bysort constituency_name : gen thirdplace66 = third[3]

* Generate margins of victory

bysort constituency_name : gen margin = (share[1] - share[2])*100

* Generate effective number of electoral parties

gen sqrvotepr = (share)^2

bysort constituency_name : egen sumsqrvotepr = sum(sqrvotepr)

gen enep = 1/sumsqrvotepr

* Labelling parties

replace party_name = "1" if party_name == "Conservative"
replace party_name = "2" if party_name == "Labour"
replace party_name = "3" if party_name == "Liberal"
replace party_name = "4" if party_name == "Nationalists"
replace party_name = "5" if party_name == "Other"

destring (party_name), replace

replace winner66 = "1" if winner66 == "Conservative"
replace winner66 = "2" if winner66 == "Labour"
replace winner66 = "3" if winner66 == "Liberal"
replace winner66 = "4" if winner66 == "Nationalists"
replace winner66 = "5" if winner66 == "Other"

destring (winner66), replace

replace runnerup66 = "1" if runnerup66 == "Conservative"
replace runnerup66 = "2" if runnerup66 == "Labour"
replace runnerup66 = "3" if runnerup66 == "Liberal"
replace runnerup66 = "4" if runnerup66 == "Nationalists"
replace runnerup66 = "5" if runnerup66 == "Other"

destring (runnerup66), replace

replace thirdplace66 = "1" if thirdplace66 == "Conservative"
replace thirdplace66 = "2" if thirdplace66 == "Labour"
replace thirdplace66 = "3" if thirdplace66 == "Liberal"
replace thirdplace66 = "4" if thirdplace66 == "Nationalists"
replace thirdplace66 = "5" if thirdplace66 == "Other"

destring (thirdplace66), replace

label define party 1 "Conservative" 2 "Labour" 3 "Liberal" 4 "Nationalists" 5 "Other"
label values party_name winner66 runnerup66 thirdplace66 party
 
* Selecting, ordering, and sorting variables

keep id constituency_name party_name rank votes share margin enep winner66 runnerup66 thirdplace66

order id constituency_name party_name rank votes share margin enep winner66 runnerup66 thirdplace66

sort id rank

drop if rank > 3

* Clean district names

replace constituency_name = upper(constituency_name)

* Drop duplicates

duplicates tag id, gen(dup)
duplicates drop id, force
drop dup

* Saving file

save UK66_results, replace

* Merge with UK64_results.dta and generate reelection variable

merge 1:1 _n using UK64_results.dta

* Generate variable for incumbent party reelection

gen reelected66 = 1 if winner66 == winner64
replace reelected66 = 0 if reelected66!=1

* Speaker's constituency

replace share = 0.85378363228 if constituency_name == "SOUTHAMPTON,ITCHEN"
replace margin = 70.756726 if constituency_name == "SOUTHAMPTON,ITCHEN"
replace enep = 1.33275453237 if constituency_name == "SOUTHAMPTON,ITCHEN"
replace party_name = 2 if constituency_name == "SOUTHAMPTON,ITCHEN"
replace winner66 = 2 if constituency_name == "SOUTHAMPTON,ITCHEN"
replace runnerup66 = . if constituency_name == "SOUTHAMPTON,ITCHEN"
replace thirdplace66 = . if constituency_name == "SOUTHAMPTON,ITCHEN"
replace reelected66 = 1 if constituency_name == "SOUTHAMPTON,ITCHEN"

* Saving file

save UK6466_results, replace

log close